package com.liuwei.assemble.services.impl;

import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.liuwei.assemble.common.GIOUtils;
import com.liuwei.assemble.constants.PathConstants;
import com.liuwei.assemble.dtos.GeneratorContextDTO;
import com.liuwei.assemble.services.IGeneratorProject;

import org.apache.velocity.VelocityContext;

import java.io.File;
import java.io.IOException;
import java.util.List;


/**
 * @author liuwei19
 */
public class SpringBootGenerator implements IGeneratorProject {
	private String src_main_java = "/src/main/java/";
	private String src_main_resource = "/src/main/resources/";
    @Override
    public void generator(GeneratorContextDTO generatorParam) throws IOException {
        PackageConfig packageConfig = generatorParam.getPackageConfig();
        DataSourceConfig sourceConfig = generatorParam.getDataSourceConfig();
        
        VelocityContext context = new VelocityContext();
        context.put("projectName",generatorParam.getProjectName());
        context.put("parentPackage",packageConfig.getParent());
        context.put("mapperScan",packageConfig.getParent()+"."+packageConfig.getMapper()+"*");
        context.put("mapperXml",packageConfig.getXml());
        context.put("url",sourceConfig.getUrl());
        context.put("username",sourceConfig.getUsername());
        context.put("password",sourceConfig.getPassword());
        context.put("driverClassName",generatorParam.getDriverClassName());
        context.put("author",generatorParam.getAuthor());
        context.put("idGenerator",generatorParam.getIdGenerator());
        context.put("configPackage",packageConfig.getParent()+".config");
        context.put("controllerPackage",packageConfig.getParent()+"."+packageConfig.getController());
        context.put("dataSourceType",sourceConfig.getDbType().getDb().replaceAll("_",""));
        List<TableInfo> tableInfos = generatorParam.getTableInfos();
        context.put("tableInfos",tableInfos);
        context.put("requestVo",generatorParam.getRequestVO());
        
        //创建build.gradle
        File rootFile = GIOUtils.checkOrCreateDir(generatorParam.getRootPath());
        //GIOUtils.vmToFile(context,PathConstants.TEMPLATE_GRADLE,rootFile.getPath()+"/build.gradle");
        GIOUtils.vmToFile(context,PathConstants.TEMPLATE_MAVEN,rootFile.getPath()+"/pom.xml");
        //创建application.yml
        String resourcePath = generatorParam.getResourcePath();
        File resourceFile = GIOUtils.checkOrCreateDir(resourcePath);
        GIOUtils.vmToFile(context,PathConstants.TEMPLATE_APPLICATION_YML,resourceFile.getPath()+"/application.yml");
        //创建bootstrap.yml
        //IoUtils.vmToFile(context,GeneratorFolderConstants.TEMPLATE_BOOTSTRAP_YML,file.getPath()+"/bootstrap.yml");
        //创建disconf.xml
        //IoUtils.vmToFile(context,GeneratorFolderConstants.TEMPLATE_DISCONF_XML,file.getPath()+"/disconf.xml");
        //创建tgop
        //IoUtils.vmToFile(context, GeneratorFolderConstants.TEMPLATE_TGOP_MEASHUREMENT, file.getPath() + "/tgop.properties");
        //创建disconf.properties
        //IoUtils.vmToFile(context,GeneratorFolderConstants.TEMPLATE_DISCONF_PROPERTIES,file.getPath()+"/disconf.properties");
        //创建tgop日志elk
        if("tgoplog".equals(generatorParam.getRequestVO().getLogtype())){
            //GIOUtils.vmToFile(context,PathConstants.TEMPLATE_PLUGIN_TGOP_LOG,file.getPath()+"/tgop.properties");
            GIOUtils.vmToFile(context,PathConstants.TEMPLATE_PLUGIN_TGOP_LOG4j,resourceFile.getPath()+"/log4j.properties");
        }else{
            GIOUtils.vmToFile(context,PathConstants.TEMPLATE_PLUGIN_LOGBACK,resourceFile.getPath()+"/logback.xml");
        }
        String parentPackage = packageConfig.getParent().replaceAll("\\.","/");

        //创建Application.java
        File javaFile = GIOUtils.checkOrCreateDir(generatorParam.getRootPath()+src_main_java+parentPackage);
        GIOUtils.vmToFile(context,PathConstants.TEMPLATE_APPLICATION_JAVA,javaFile.getPath()+"/Application.java");
        
        File adminFile = GIOUtils.checkOrCreateDir(generatorParam.getRootPath()+src_main_java+parentPackage+"/admin");
        GIOUtils.vmToFile(context,PathConstants.TEMPLATE_INDEX_CONTROLLER,adminFile.getPath()+"/IndexController.java");

        //创建config配置
        File configFile = GIOUtils.checkOrCreateDir(generatorParam.getRootPath()+src_main_java+parentPackage+"/config");
        GIOUtils.vmToFile(context,PathConstants.TEMPLATE_COMMONCONFIG_JAVA,configFile.getPath()+"/CommonConfig.java");
        GIOUtils.vmToFile(context,PathConstants.TEMPLATE_ASPECT_JAVA,configFile.getPath()+"/AspectConfig.java");
        GIOUtils.vmToFile(context,PathConstants.TEMPLATE_DATA_SOURCE_JAVA,configFile.getPath()+"/DruidDataSourceConfig.java");
        
        //创建common包
        File commonFile = GIOUtils.checkOrCreateDir(generatorParam.getRootPath()+src_main_java+parentPackage+"/common");
        GIOUtils.vmToFile(context,PathConstants.TEMPLATE_BASE_CONTROLLER,commonFile.getPath()+"/BaseController.java");
        GIOUtils.vmToFile(context,PathConstants.TEMPLATE_RES_CODE,commonFile.getPath()+"/ResponseCodeEnum.java");
        GIOUtils.vmToFile(context,PathConstants.TEMPLATE_RES_VO,commonFile.getPath()+"/ResponseVO.java");
        
        //复制xml文件目录
        File sourceXmlFile = GIOUtils.checkOrCreateDir(generatorParam.getGlobalConfig().getOutputDir()+"/"+parentPackage+"/"+packageConfig.getXml());
        File targetXmlFile = GIOUtils.checkOrCreateDir(generatorParam.getRootPath()+src_main_resource+packageConfig.getXml());
        GIOUtils.copyFolder(sourceXmlFile.getPath(),targetXmlFile.getPath());
        GIOUtils.deleteDir(sourceXmlFile);

        //创建静态数据
        //file = IoUtils.checkOrCreateDir(generatorParam.getRootPath()+"/src/main/resources/i18n");
        //String sourcePath = IoUtils.getTemplatePath()+"/i18n";
        //IoUtils.copyFolder(sourcePath,file.getPath());
        
        //重写Controller
    }
}
